﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Hotel.ThongKe
{
    public partial class ThongKeDoanhThu : Form
    {
        private SqlConnection conn = new SqlConnection(
               "Server=localhost;DataBase=Hotel;Integrated Security=SSPI");
        private HotelDataSet ds = new HotelDataSet();
        private SqlDataAdapter da;
        private SqlDataReader dr = null;
        SqlCommand cmd = null;
        Double tongthu = 0;
        double tongchi = 0;
        Double loinhuan = 0;
        String sql1 = null;
        String sql2 = null;
        double ngay_1;
        double ngay_2;
        double thang_1;
        double thang_2;
        double nam_1;
        double nam_2;
        public ThongKeDoanhThu()
        {
            InitializeComponent();
           
        }

        private void btnThongKe_Click(object sender, EventArgs e)
        {
           if (dtpNgaybatdau.Value > DateTime.Now || dtpNgaytinh.Value > DateTime.Now || dtpNgaybatdau.Value > dtpNgaytinh.Value)
                MessageBox.Show("Chọn ngày thống kê không phù hợp!");
            else
            {
                ngay_1 = Convert.ToDouble(Convert.ToString(dtpNgaybatdau.Value.Date.Day));
                thang_1 = Convert.ToDouble(Convert.ToString(dtpNgaybatdau.Value.Date.Month));
                nam_1 = Convert.ToDouble(Convert.ToString(dtpNgaybatdau.Value.Date.Year));
                String ngaybatdau = ngay_1 + "/" + thang_1 + "/" + nam_1;

                ngay_2 = Convert.ToDouble(Convert.ToString(dtpNgaytinh.Value.Date.Day));
                thang_2 = Convert.ToDouble(Convert.ToString(dtpNgaytinh.Value.Date.Month));
                nam_2 = Convert.ToDouble(Convert.ToString(dtpNgaytinh.Value.Date.Year));
                String ngaytinh = ngay_2 + "/" + thang_2 + "/" + nam_2;

                sql1 = "select  sum ( SoTien )from HoaDonChi where (DaXoa='0' and (convert(datetime,NgayChi,103) between convert(datetime,'" + ngaybatdau + "',103) and convert(datetime,'" + ngaytinh + "',103)))";
                sql2 = "select sum(TongTienPhong) from HoaDonPhong where(DaXoa='0' and ( convert(datetime,NgayLap,103) between convert(datetime,'" + ngaybatdau + "',103) and convert(datetime,'" + ngaytinh + "',103)))";
                try
                {
                    conn.Open();
                    cmd = new SqlCommand(sql1, conn);
                    tongchi = Convert.ToDouble(cmd.ExecuteScalar());
                    //tbTongChi.Text = Convert.ToString(tongchi);
                    cmd = new SqlCommand(sql2, conn);
                    tongthu = Convert.ToDouble(cmd.ExecuteScalar());


                }
                catch
                {

                }
                finally
                {
                    if (dr != null)
                    {
                        dr.Close();
                    }
                    if (conn != null)
                    {
                        conn.Close();
                    }
                }
                loinhuan = tongthu - tongchi;
                tbLoinhuan.Text = Convert.ToString(loinhuan);
                tbTongChi.Text = Convert.ToString(tongchi);
                tbTongThu.Text = Convert.ToString(tongthu);
                da = new SqlDataAdapter("SELECT MaHDC,TenHDC, NgayChi, SoTien, NhanVien.TenNV FROM  HoaDonChi INNER JOIN  NhanVien ON HoaDonChi.MaNV = NhanVien.MaNV where (HoaDonChi.DaXoa='0' and (convert(datetime,HoaDonChi.NgayChi,103) between convert(datetime,'" + ngaybatdau + "',103) and convert(datetime,'" + ngaytinh + "',103))) ", conn);
                DataTable tb = new DataTable();
                da.Fill(tb);
                dtgvDanhsach_Chi.DataSource = tb;
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        
    }
}
